/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import EntityBeans.Client;
import SessionBeans.ClientFacadeLocal;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import obj.Md5;

/**
 *
 * @author Nascotix
 */
@ManagedBean(name="login" )
@SessionScoped
public class Login {
    @EJB
    private ClientFacadeLocal clientFacade;
    
    //Variables
    private String userLogin;
    private String userPwd;
    private Client currentUser = null;
    private String longName;
    
    public Login(){}
    
    
    //Getter/Setter
    public String getUserLogin() {
        return userLogin;
    }
    public void setUserLogin(String userLogin) {
        this.userLogin = userLogin;
    }
    public String getUserPwd() {
        return userPwd;
    }
    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }
    public Client getCurrentUser() {
        return currentUser;
    }
    public void setCurrentUser(Client currentUser) {
        this.currentUser = currentUser;
    }
        public String getLongName() {
        return longName;
    }
    public void setLongName(String longName) {
        this.longName = longName;
    }
    
    //Fonctions 
    public String loginToAccount() {
        try{
            Client client = clientFacade.findClientbyMail(userLogin);

            Md5 passmd5 = new Md5();
            //System.out.println(client.getPassword());
            System.out.println(passmd5.convert(userPwd));
            if(client.getPassword().equals(passmd5.convert(userPwd)))
            {
              currentUser = client;
              longName = client.getFirstname() + " " + client.getName();
            }
            else
            {
                //System.out.println("Fail");
                currentUser = null;
            }
             return null;
        }
        catch(Exception e){
            return "NewAccount";
        } 
    }
    
    public int logoutUser(){
        currentUser = null;
        return 1;
    }
    
     public int loginState(){
        if (currentUser == null)
        {
            return 0;
        }
        else
        {   
            return 1;
        }
    }
}
